package com.android.pantryshopping;

import android.content.ContentValues;
import android.content.Context;
import android.database.AbstractWindowedCursor;
import android.database.Cursor;
import android.database.CursorWindow;
import android.database.sqlite.SQLiteBlobTooBigException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.android.pantryshopping.bean.Commodity;
import com.android.pantryshopping.bean.ShopList;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

public class CommoditySqLiteHelper extends SQLiteOpenHelper {

    private final String TABLE_NAME_COMMODITY = "commodity";
    private final String TABLE_NAME_SHOP_LIST = "shopList";

    /*id字段*/
    private final String VALUE_ID = "_id";
    private final String VALUE_comID = "commodityId";
    private final String VALUE_PIC = "pic";
    private final String VALUE_NAME = "name";
    private final String VALUE_PRICE = "price";
    private final String VALUE_NUM = "num";

    private final String VALUE_SHOP_ID = "shopId";
    private final String VALUE_SHOP_NAME = "shopName";
    private final String VALUE_SHOP_PRICE = "priceTotal";
    private final String VALUE_SHOP_MALL_LIST = "mallList";

    private final String CREATE_COMMODITY = "create table " + TABLE_NAME_COMMODITY + "(" +
            VALUE_ID + " integer primary key," +
            VALUE_comID + " text ," +
            VALUE_PIC + " text ," +
            VALUE_NAME + " text ," +
            VALUE_PRICE + " text," +
            VALUE_NUM + " text" +
            ")";

    private final String CREATE_SHOP_LIST = "create table " + TABLE_NAME_SHOP_LIST + "(" +
            VALUE_ID + " integer primary key," +
            VALUE_SHOP_ID + " text ," +
            VALUE_SHOP_NAME + " text, " +
            VALUE_SHOP_PRICE + " int, " +
            VALUE_SHOP_MALL_LIST + " text " +
            ")";

    public CommoditySqLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_COMMODITY);
        db.execSQL(CREATE_SHOP_LIST);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    /**
     * @param model 数据模型
     * @return 返回添加数据有木有成功
     */
    public boolean addCommodityData(Commodity model) {
        //把数据添加到ContentValues
        ContentValues values = new ContentValues();
        values.put(VALUE_comID, model.getId());
        values.put(VALUE_NAME, model.getName());
        values.put(VALUE_PRICE, model.getPrice());
        values.put(VALUE_NUM, model.getQuantity());
        values.put(VALUE_PIC, model.getImage());

        //添加数据到数据库
        long index = getWritableDatabase().insert(TABLE_NAME_COMMODITY, null, values);

        //大于0表示添加成功
        return index > 0;
    }

    public boolean addShopListData(ShopList model) {
        //把数据添加到ContentValues
        ContentValues values = new ContentValues();
        values.put(VALUE_SHOP_ID, model.getId());
        values.put(VALUE_SHOP_NAME, model.getName());
        values.put(VALUE_SHOP_PRICE, model.getCommodityList());
        values.put(VALUE_SHOP_MALL_LIST, model.getCheckList());

        //添加数据到数据库
        long index = getWritableDatabase().insert(TABLE_NAME_SHOP_LIST, null, values);

        //大于0表示添加成功
        return index > 0;
    }

    /**
     * 方法删除数据库数据
     */
    public void deleteShopListData(ShopList model) {
        getWritableDatabase().delete(TABLE_NAME_SHOP_LIST, VALUE_SHOP_ID + "=?", new String[]{"" + model.getId()});
    }

    /**
     * 方法删除数据库数据
     */
    public void deleteCommodityData(Commodity model) {
        getWritableDatabase().delete(TABLE_NAME_COMMODITY, VALUE_comID + "=?", new String[]{"" + model.getId()});
    }

    /**
     * 方法修改数据库数据
     */
    public void updateCommodityData(Commodity model) {
        //将数据添加至ContentValues
        ContentValues values = new ContentValues();
        values.put(VALUE_comID, model.getId());
        values.put(VALUE_NAME, model.getName());
        values.put(VALUE_PRICE, model.getPrice());
        values.put(VALUE_NUM, model.getQuantity());
        values.put(VALUE_PIC, model.getImage());

        //修改model的数据
        getWritableDatabase().update(TABLE_NAME_COMMODITY, values, VALUE_comID + "=?", new String[]{"" + model.getId()});
    }

    /**
     * 方法修改数据库数据
     */
    public void updateShopListData(ShopList model) {
        //将数据添加至ContentValues
        ContentValues values = new ContentValues();
        values.put(VALUE_SHOP_ID, model.getId());
        values.put(VALUE_SHOP_NAME, model.getName());
        values.put(VALUE_SHOP_PRICE, model.getCommodityList());
        values.put(VALUE_SHOP_MALL_LIST, model.getCheckList());

        //修改model的数据
        getWritableDatabase().update(TABLE_NAME_SHOP_LIST, values, VALUE_SHOP_ID + "=?", new String[]{"" + model.getId()});
    }


    /**
     * 查询全部数据
     */
    public List<Commodity> queryAllCommodityData() {

        //查询全部数据
        Cursor cursor = getWritableDatabase().query(TABLE_NAME_COMMODITY, null, null, null, null, null, null, null);
        List<Commodity> list = new ArrayList<>();
        if (cursor.getCount() > 0) {
            //移动到首位
            cursor.moveToFirst();
            for (int i = 0; i < cursor.getCount(); i++) {

                String comID = cursor.getString(cursor.getColumnIndex(VALUE_comID));
                String name = cursor.getString(cursor.getColumnIndex(VALUE_NAME));
                String price = cursor.getString(cursor.getColumnIndex(VALUE_PRICE));
                String num = cursor.getString(cursor.getColumnIndex(VALUE_NUM));
                String pic = cursor.getString(cursor.getColumnIndex(VALUE_PIC));

                Commodity model = new Commodity();
                model.setId(comID);
                model.setName(name);
                model.setPrice(price);
                model.setQuantity(num);
                model.setImage(pic);

                list.add(model);
                //移动到下一位
                cursor.moveToNext();
            }
        }
        cursor.close();
        getWritableDatabase().close();
        return list;
    }

    /**
     * 查询全部数据
     */
    public List<ShopList> queryAllShopListData() {

        //查询全部数据
        Cursor cursor = getWritableDatabase().query(TABLE_NAME_SHOP_LIST, null, null, null, null, null, null, null);
        List<ShopList> list = new ArrayList<>();
        if (cursor.getCount() > 0) {
            //移动到首位
            cursor.moveToFirst();
            for (int i = 0; i < cursor.getCount(); i++) {

                String shopID = cursor.getString(cursor.getColumnIndex(VALUE_SHOP_ID));
                String shopName = cursor.getString(cursor.getColumnIndex(VALUE_SHOP_NAME));
                int priceTotal = cursor.getInt(cursor.getColumnIndex(VALUE_SHOP_PRICE));
                String mallList = cursor.getString(cursor.getColumnIndex(VALUE_SHOP_MALL_LIST));

                ShopList model = new ShopList();
                model.setId(shopID);
                model.setName(shopName);
                model.setCommodityList(priceTotal);
                model.setCheckList(mallList);

                list.add(model);
                //移动到下一位
                cursor.moveToNext();
            }
        }
        cursor.close();
        getWritableDatabase().close();
        return list;
    }
}
